package edu.whu.demo.service;

import ch.qos.logback.core.db.dialect.DBUtil;
import edu.whu.demo.entity.Product;
import edu.whu.demo.entity.Supplier;
import edu.whu.demo.repository.productRepo;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.provider.HibernateUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProductService {




    @Autowired
    productRepo productRepo;

    /**
     * 添加商品
     * @param product
     * @return
     */
    public Product addProduct(Product product) {

//        productMap.put(product.getId(), product);
//        return product;
        return productRepo.save(product);
    }

    /**
     * 根据Id查找
     * @param id
     * @return
     */
    public Product getProduct(long id) {
//        return productMap.get(id);
        Optional<Product> product = productRepo.findById(id);
        if(product.isPresent()){
            return product.get();
        }
        return null;

    }

    /**
     * 根据名称和库存查找
     * @param name
     * @param quantity
     * @return
     */
    public List<Product> findProduct(String name, Float quantity) {
//        List<Product> result=new ArrayList<>();
//        for (Product product: productMap.values()){
//            if (name!=null && !product.getName().contains(name)) {
//                continue;
//            }
//            if (quantity!=null && product.getStockQuantity()>= quantity) {
//                continue;
//            }
//            result.add(product);
//        }
//        return result;
        List<Product>result=new ArrayList<>();
        for(Product product:productRepo.findAll()){
            if(name!=null&&!product.getName().contains(name)) {
                continue;
            }
            if(quantity!=null&&product.getStockQuantity()>=quantity) {
                continue;
            }
            result.add(product);
        }
        return result;
    }

    /**
     * 更新商品信息
     * @param id
     * @param product
     */
    public void updateProduct(long id, Product product) {
//        Product product1  = productMap.get(id);
//        if(product1!=null){
//            productMap.put(product.getId(),product);
//        }
        if(productRepo.findById(id).isPresent()){
            productRepo.save(product);
        }

    }

    /**
     * 删除商品
     * @param id
     */
    public void deleteProduct(long id) {
//        productMap.remove(id);
        productRepo.deleteById(id);
    }

    /**
     * 删除所有商品
     */
    public void deleteAll(){
//        productMap.clear();
        productRepo.deleteAll();
    }

    /**
     * 根据商品id查询所有供货商
     */
    public List<Supplier>findSupById(long id){
        Optional<Product> product = productRepo.findById(id);
        List<Supplier>suppliers=new ArrayList<>();
        if(product.isPresent()){
            suppliers.addAll(product.get().getSuppliers());
        }
        return suppliers;

    }

}
